﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Controls;
using LMD.Lib.Oracle;

public partial class FeedUnsubscribe : ICPGeneralPage
{
    public DataTable m_dtSource = new DataTable();

    #region Properties
    #endregion Properties

    #region Event Handlers
    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Pengaturan Feed Source";
        InitGrid(!IsPostBack && !IsCallback);
    }
    
    protected void dgMain_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        UpdateFeedUnsubscribe();
    }
    #endregion Event Handlers

    #region Custom Methods
    private void InitGrid(bool bRefresh)
    {
        Cached.FeedUnsubscribe ccTmp = new Cached.FeedUnsubscribe();
        m_dtSource = ccTmp.GetFeedUnsubscribeList(m_sxUID, ICPUser.RoleId, bRefresh);

        dgMain.Caption = "ICP Feed UnSubscription by User Role";

        dgMain.DataSource = m_dtSource;
        
        if (bRefresh)
        {
            for (int i = 0; i < m_dtSource.Rows.Count; i++)
            {
                dgMain.Selection.SetSelectionByKey(
                    m_dtSource.Rows[i]["SRC_ID"],
                    m_dtSource.Rows[i]["IS_CHECKED"].ToString() == KGlobal.YES);
            }
        }

        dgMain.DataBind();
    }

    private void UpdateFeedUnsubscribe()
    {
        Cached.FeedUnsubscribe ccTmp = new Cached.FeedUnsubscribe();
        DataTable dtOld = ccTmp.GetFeedUnsubscribeList(m_sxUID, ICPUser.RoleId, true);
        DataTable dtUpdate = (DataTable)dgMain.DataSource;

        DBLib dbl = new DBLib();

        dbl.BeginTransaction();
        try
        {
            for (int i = 0; i < dtOld.Rows.Count; i++)
            {
                bool IsOldSelected = dtOld.Rows[i]["IS_CHECKED"].ToString() == KGlobal.YES;
                int iFeedSourceId = dtOld.Rows[i].ReadInt("SRC_ID");
                bool IsNewSelected = dgMain.Selection.IsRowSelectedByKey(dtOld.Rows[i]["SRC_ID"]);

                if (IsOldSelected && !IsNewSelected)
                {
                    DBQuery.FeedUnsubscribeTable.AddFeedUnsubscribe(dbl, iFeedSourceId, m_sxUID);
                }
                else if (!IsOldSelected && IsNewSelected)
                {
                    DBQuery.FeedUnsubscribeTable.DeleteFeedUnsubscribe(dbl, iFeedSourceId, m_sxUID);
                }
            }

            dbl.EndTransaction(true);

            InitGrid(true);
        }
        catch (Exception ex)
        {
            dbl.EndTransaction(false);
            throw ex;
        }
    }

    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    #endregion Custom Methods

    
}
